﻿using Microsoft.Xna.Framework;
using SomeGame.GUI.Widgets;

namespace SomeGame.GUI.WidgetActions {

    public class SlideAction : WidgetAction {

        public enum WidgetSlideDirection {

            Up,
            Right,
            Down,
            Left

        }

        private readonly int Destination;
        private readonly WidgetSlideDirection Direction;
        private readonly int Speed;

        public SlideAction(Widget widget,
            WidgetAction parent,
            WidgetActionState state,
            WidgetSlideDirection direction,
            int destination,
            int speed) : base(widget, parent, state) {
            Direction = direction;
            Destination = destination;
            Speed = speed;
        }

        public override void Update(GameTime gameTime) {
            /*
             * This 'Destination' thing is necessary because the Widget's Top or Left
             * needs to progress naturally in order to be referenced correctly when
             * more than one Slide is chained. In other words, the chained Slides have
             * no way to know where the previous Slides intend to go, all they can do
             * is check if the Widget is at THEIR destination when they take over.
             */
            switch (Direction) {
                case WidgetSlideDirection.Up:
                    Widget.Top -= Speed;
                    if (Widget.Top <= Destination) {
                        Widget.Top = Destination;
                        State = WidgetActionState.Done;
                    }
                    break;
                case WidgetSlideDirection.Right:
                    Widget.Left += Speed;
                    if (Widget.Left >= Destination) {
                        Widget.Left = Destination;
                        State = WidgetActionState.Done;
                    }
                    break;
                case WidgetSlideDirection.Down:
                    Widget.Top += Speed;
                    if (Widget.Top >= Destination) {
                        Widget.Top = Destination;
                        State = WidgetActionState.Done;
                    }
                    break;
                case WidgetSlideDirection.Left:
                    Widget.Left -= Speed;
                    if (Widget.Left <= Destination) {
                        Widget.Left = Destination;
                        State = WidgetActionState.Done;
                    }
                    break;
            }
            base.Update(gameTime);
        }

    }

}